/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__do_l7_del.c                                     */
/*  NOTE      =                                                    */
/*  DATE      = 2014/12/31 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>
#include <protobuf-c/protobuf-c.h>

#include "frcomm.h"
#include "frmem.h"
#include "frame.h"
#include "client.pb-c.h"
#include "frsutl.h"
#include "frcmd_server_l7.h"
#include "frame_params.h"
#include "apmtest_cunit.h"

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

int __do_l7_del(L7Setting *l7_settings);

//临时文件不存在
void case1(void)
{
    int i_ret = 0;
    L7Setting *l7_settings = NULL;

    //设置l7_settings
    l7_settings = frmalloc(sizeof(L7Setting));
    l7_setting__init(l7_settings);
    l7_settings->has_ip_proto = 1;
    l7_settings->ip_proto = IPPROTO_TCP;
    l7_settings->ip = iptools_parse_ipaddr("192.168.18.61");
    l7_settings->port = frmalloc(10);
    snprintf(l7_settings->port,10,"%s","1433-1445");
    l7_settings->l7_proto = frmalloc(5);
    snprintf(l7_settings->l7_proto,5,"%s","1433");

    system("cp ./conf/test1.txt ./conf/analyzer.conf");
    i_ret = __do_l7_del(l7_settings);
    system("cp ./conf/analyzer.conf_tmp ./conf/test1_case1.txt");
    system("rm ./conf/analyzer.conf_tmp");
    system("rm ./conf/analyzer.conf");

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(l7_settings->port);
    frfree(l7_settings->l7_proto);
    frfree(l7_settings);
    return;
}

//临时文件存在
void case2(void)
{
    int i_ret = 0;
    L7Setting *l7_settings = NULL;

    //设置l7_settings
    l7_settings = frmalloc(sizeof(L7Setting));
    l7_setting__init(l7_settings);
    l7_settings->has_ip_proto = 1;
    l7_settings->ip_proto = IPPROTO_TCP;
    l7_settings->ip = iptools_parse_ipaddr("192.168.18.61");
    l7_settings->port = frmalloc(10);
    snprintf(l7_settings->port,10,"%s","1433-1445");
    l7_settings->l7_proto = frmalloc(5);
    snprintf(l7_settings->l7_proto,5,"%s","1433");

    system("cp ./conf/test1.txt ./conf/analyzer.conf_tmp");
    system("cp ./conf/test1.txt ./conf/analyzer.conf");
    i_ret = __do_l7_del(l7_settings);
    system("cp ./conf/analyzer.conf_tmp ./conf/test1_case2.txt");
    system("rm ./conf/analyzer.conf_tmp");
    system("rm ./conf/analyzer.conf");

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(l7_settings->port);
    frfree(l7_settings->l7_proto);
    frfree(l7_settings);
    return;
}

//临时文件存在 但无KYE
void case3(void)
{
    int i_ret = 0;
    L7Setting *l7_settings = NULL;

    //设置l7_settings
    l7_settings = frmalloc(sizeof(L7Setting));
    l7_setting__init(l7_settings);
    l7_settings->has_ip_proto = 1;
    l7_settings->ip_proto = IPPROTO_TCP;
    l7_settings->ip = iptools_parse_ipaddr("192.168.18.61");
    l7_settings->port = frmalloc(10);
    snprintf(l7_settings->port,10,"%s","1433-1445");
    l7_settings->l7_proto = frmalloc(5);
    snprintf(l7_settings->l7_proto,5,"%s","1433");

    system("cp ./conf/test2.txt ./conf/analyzer.conf_tmp");
    system("cp ./conf/test1.txt ./conf/analyzer.conf");
    i_ret = __do_l7_del(l7_settings);
    system("cp ./conf/analyzer.conf_tmp ./conf/test2_case3.txt");
    system("rm ./conf/analyzer.conf_tmp");
    system("rm ./conf/analyzer.conf");

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(l7_settings->port);
    frfree(l7_settings->l7_proto);
    frfree(l7_settings);
    return;
}


void case4(void)
{
    int i_ret = 0;
    L7Setting *l7_settings = NULL;

    //设置l7_settings
    l7_settings = frmalloc(sizeof(L7Setting));
    l7_setting__init(l7_settings);
    l7_settings->has_ip_proto = 0;
    l7_settings->ip = iptools_parse_ipaddr("192.168.18.61");
    l7_settings->port = frmalloc(10);
    snprintf(l7_settings->port,10,"%s","1433-1445");
    l7_settings->l7_proto = frmalloc(5);
    snprintf(l7_settings->l7_proto,5,"%s","1433");

    system("cp ./conf/test3.txt ./conf/analyzer.conf_tmp");
    system("cp ./conf/test1.txt ./conf/analyzer.conf");
    i_ret = __do_l7_del(l7_settings);
    system("cp ./conf/analyzer.conf_tmp ./conf/test3_case4.txt");
    system("rm ./conf/analyzer.conf_tmp");
    system("rm ./conf/analyzer.conf");

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(l7_settings->port);
    frfree(l7_settings->l7_proto);
    frfree(l7_settings);
    return;
}


int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("t__do_l7_del 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);
    CU_add_test(pSuite, "case2", case2);
    CU_add_test(pSuite, "case3", case3);
    CU_add_test(pSuite, "case4", case4);
//    CU_add_test(pSuite, "case5", case5);
//    CU_add_test(pSuite, "case6", case6);
//    CU_add_test(pSuite, "case7", case7);
//    CU_add_test(pSuite, "case8", case8);

    return 0;

}

int main(int argc, char **argv)
{
    frmem_init();
    run_cunit(add_suite);
    frshow_leak();
    frmem_destroy();
    return 0;
}

